package cn.stylefeng.guns.modular.tool.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.tool.entity.ToolExpressReconciliationZtx;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationJtParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationRfParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationZtxParam;
import cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationRfService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationZtxService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * 中通对账管理新控制器
 *
 * @author zhucx
 * @Date 2023-05-30 14:43:18
 */
@Controller
@RequestMapping("/toolExpressReconciliationZtx")
public class ToolExpressReconciliationZtxController extends BaseController {

    private String PREFIX = "/tool/toolExpressReconciliationZtx";

    @Autowired
    private ToolExpressReconciliationZtxService toolExpressReconciliationZtxService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/toolExpressReconciliationZtx.html";
    }
    
    /**
     * 跳转到导入 	对账	页面
     */
    @RequestMapping("toimportExcel")
    public String toimportExcel() {
        return PREFIX + "/import.html";
    }
    
    /**
     * 快递明细
     * 上传excel   
     */
	@RequestMapping(value = "/importExceNewmethodl" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importExceNewmethodl(@RequestPart("file") MultipartFile file,Long reconciliationId)  {
		ResponseData data = new ResponseData();
		try {
			data = toolExpressReconciliationZtxService.getExceldata(file,reconciliationId,data);//处理导入数据
		} catch (Exception e) {
			e.printStackTrace();
		} 
        return data;
    }
    
    /**
     * 下载订单模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcelTemplate")
    public void downloadExcelTemplate(HttpServletRequest request,HttpServletResponse response) throws ServletException ,IOException {
        String path = "assets/excel/toolExpressReconciliationZtx.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream  outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("中通对账导入模板新" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 导出极兔核对账单Excel
     */
    @RequestMapping("/exportToExcel")
    public void exportToExcel(HttpServletResponse response,Long reconciliationId) throws IOException {
        Workbook wb = toolExpressReconciliationZtxService.exportToExcel(reconciliationId);
        OutputStream output = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(("中通成本核对账单新" + ".xlsx").getBytes(), "iso-8859-1"));
        wb.write(output);
        output.close();
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/toolExpressReconciliationZtx_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/toolExpressReconciliationZtx_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolExpressReconciliationZtxParam toolExpressReconciliationZtxParam) {
    	User user = userService.getcurrentUser();
    	toolExpressReconciliationZtxParam.setDeptId(user.getDeptId());
    	toolExpressReconciliationZtxParam.setDeliverId(user.getDeliverId());
    	toolExpressReconciliationZtxParam.setCreateUserName(user.getName());
        this.toolExpressReconciliationZtxService.add(toolExpressReconciliationZtxParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ToolExpressReconciliationZtxParam toolExpressReconciliationZtxParam) {
        this.toolExpressReconciliationZtxService.update(toolExpressReconciliationZtxParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ToolExpressReconciliationZtxParam toolExpressReconciliationZtxParam) {
        this.toolExpressReconciliationZtxService.delete(toolExpressReconciliationZtxParam);
        
        ToolExpressReconciliationZtxParam ToolExpressReconciliationZtxParam2 = new ToolExpressReconciliationZtxParam();
        ToolExpressReconciliationZtxParam2.setReconciliationId(Long.parseLong(toolExpressReconciliationZtxParam.getId().toString()));
		this.toolExpressReconciliationZtxService.deleteBySpec(ToolExpressReconciliationZtxParam2);
		
		ToolExpressReconciliationRfParam toolExpressReconciliationRfParam = new ToolExpressReconciliationRfParam();
		toolExpressReconciliationRfParam.setV1("ztx");
		toolExpressReconciliationRfParam.setReconciliationId(Long.parseLong(toolExpressReconciliationZtxParam.getId().toString()));
		this.toolExpressReconciliationRfService.deleteBySpec(toolExpressReconciliationRfParam);
        return ResponseData.success();
    }

    @Autowired
    private ToolExpressReconciliationRfService toolExpressReconciliationRfService;

    /**
     * 清空接口
     *
     * @author zhucx
     * @Date 2022-12-14
     */
    @RequestMapping("/btnRevoke2")
    @ResponseBody
    public ResponseData btnRevoke2(ToolExpressReconciliationZtxParam toolExpressReconciliationZtxParam) {
		this.toolExpressReconciliationZtxService.deleteBySpec(toolExpressReconciliationZtxParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolExpressReconciliationZtxParam toolExpressReconciliationZtxParam) {
        ToolExpressReconciliationZtx detail = this.toolExpressReconciliationZtxService.getById(toolExpressReconciliationZtxParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolExpressReconciliationZtxParam toolExpressReconciliationZtxParam,String condition ) {
    	User user = userService.getcurrentUser();
    	toolExpressReconciliationZtxParam.setDeptId(user.getDeptId());
        return this.toolExpressReconciliationZtxService.findPageBySpec(toolExpressReconciliationZtxParam,condition,user.getOperateId());
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2023-05-30
     */
    @ResponseBody
    @RequestMapping("/list2")
    public LayuiPageInfo list2(ToolExpressReconciliationZtxParam toolExpressReconciliationZtxParam,String condition ) {
        return this.toolExpressReconciliationZtxService.findPageBySpecZtxDz(toolExpressReconciliationZtxParam,condition);
    }

}


